package com.briup.jd2407cms.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.briup.jd2407cms.domain.Subcomment;
import com.briup.jd2407cms.domain.User;
import com.briup.jd2407cms.exception.ServiceException;
import com.briup.jd2407cms.mapper.CommentMapper;
import com.briup.jd2407cms.mapper.UserMapper;
import com.briup.jd2407cms.service.SubcommentService;
import com.briup.jd2407cms.mapper.SubcommentMapper;
import com.briup.jd2407cms.util.result.ResultCode;
import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author jay
* @description 针对表【cms_subcomment】的数据库操作Service实现
* @createDate 2024-11-18 14:40:14
*/
@Service
public class SubcommentServiceImpl extends ServiceImpl<SubcommentMapper, Subcomment>
    implements SubcommentService{
    @Autowired
    private SubcommentMapper subcommentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;

    public List<Subcomment> selectByParentId(String parentId) {
        LambdaQueryWrapper<Subcomment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Subcomment::getParentId, parentId).orderByDesc(Subcomment::getPublishTime);
        return super.list(lqw);
    }

    @Override
    public boolean saveSubComment(Subcomment subComment) {
        // 1.参数判断
        if (ObjUtil.isEmpty(subComment)){
            log.error("回复评论的内容为空");
            throw new ServiceException(ResultCode.PARAM_IS_BLANK);
        }

        // 2.用户存在及禁用状态判断
        User user = userMapper.selectById(subComment.getUserId());
        if(ObjUtil.isEmpty(user)){
            log.error("回复评论的账号不存在");
            throw new ServiceException(ResultCode.USER_NOT_EXIST);
        }
        if("禁用".equals(user.getStatus())){
            log.error("回复评论的账号已被禁用");
            throw new ServiceException(ResultCode.USER_ACCOUNT_FORBIDDEN);
        }

        // 3.一级评论存在判断
        if (ObjUtil.isEmpty(commentMapper.selectById(subComment.getParentId()))){
            log.error("要回复一级的评论不存在");
            throw new ServiceException(ResultCode.COMMENT_NOT_EXIST);
        }

        // 4.回复评论id存在判断【replyId可以存在也可以不存在】
        // 存在为回复二级评论的评论，不存在为直接回复一级评论的评论
        if (ObjUtil.isEmpty(subComment.getReplyId())){
            subComment.setReplyId(null);
        }

        // 5.发表时间准备
        subComment.setPublishTime(LocalDateTime.now());

        return super.save(subComment);
    }

    @Override
    public boolean deleteByParentId(String parentId) {
        LambdaQueryWrapper<Subcomment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Subcomment::getParentId,parentId);
        return SqlHelper.retBool(subcommentMapper.delete(lqw));
    }
}




